package com.hljx.job;

import com.hljx.entity.*;
import com.hljx.mapper.sys.*;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;


/**
 * @BelongsProject: hljx
 * @BelongsPackage: com.hljx.job
 * @Author: lp
 * @CreateTime: 2023-10-23  08:56
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Configuration
public class StepItem {
    private final PlatformTransactionManager transactionManager;

    private final JobRepository jobRepository;

    private static final List<ListItem<?>> cachedItems = Arrays.asList(
            new ListItem<>(IfFeeitemlist.class),
            new ListItem<>(Ipmomdod.class),
            new ListItem<>(Ipmonmex.class),
            new ListItem<>(UndrugDetail.class),
            new ListItem<>(ClinicInvoiceInfo.class),
            new ListItem<>(IiBeduserecord.class),
            new ListItem<>(IiShiftdata.class),
            new ListItem<>(IncomeExpenses.class),
            new ListItem<>(LlOneBackExpenses.class),
            new ListItem<>(Mrmsbase.class),
            new ListItem<>(Mrmsdymr.class),
            new ListItem<>(OpApply.class),
            new ListItem<>(OrdSeedoctrecord.class),
            new ListItem<>(RMzClassCollectYard.class),
            new ListItem<>(RStatMzClassCollect.class),
            new ListItem<>(RStatMzCollect.class),
            new ListItem<>(VmHdTreat.class),
            new ListItem<>(MedOperationMaster.class),
            new ListItem<>(CpoeProjectDischargeExe.class),
            new ListItem<>(OpRecord.class),
            new ListItem<>(RRegister.class),
            new ListItem<>(VHljxFeedetail.class),
            new ListItem<>(VmHdTreatXg.class),
            new ListItem<>(vCssdWorkload.class)
            

    );




    private final IfFeeitemlistMapper ifFeeItemMapper;

    private final IpmomdodMapper ipmomdodMapper;

    private final IpmonmexMapper ipmonmexMapper;

    private final UndrugDetailMapper undrugDetailMapper;

    private final ClinicInvoiceInfoMapper clinicInvoiceInfoMapper;

    private final IiBeduserecordMapper iiBeduserecordMapper;

    private final IiShiftdataMapper iiShiftdataMapper;

    private final IncomeExpensesMapper incomeExpensesMapper;

    private final LlOneBackExpensesMapper llOneBackExpensesMapper;

    private final MrmsbaseMapper mrmsbaseMapper;

    private final MrmsdymrMapper mrmsdymrMapper;

    private final OpApplyMapper opApplyMapper;

    private final OrdSeedoctrecordMapper ordSeedoctrecordMapper;

    private final RMzClassCollectYardMapper rMzClassCollectYardMapper;

    private final RStatMzClassCollectMapper rStatMzClassCollectMapper;

    private final RStatMzCollectMapper rStatMzCollectMapper;
    private  final  VmHdTreatMapper  vmHdTreatMapper;
    private  final  VmHdTreatXgMapper  vmHdTreatXGMapper;
    private  final  MedOperationMasterMapper  medOperationMasterMapper;
    private final   CpoeProjectDischargeExeMapper cpoeProjectDischargeExeMapper;
    private final   OpRecordMapper  opRecordMapper;
    private final   RRegisterMapper  rRegisterMapper;
    private final   VHljxFeedetailMapper  vHljxFeedetailMapper;
    private final   VCssdWorkloadMapper  vCssdWorkloadMapper;


    public StepItem(JobRepository jobRepository,
                    PlatformTransactionManager transactionManager, IfFeeitemlistMapper ifFeeItemMapper,
                    IpmomdodMapper ipmomdodMapper, IpmonmexMapper ipmonmexMapper, UndrugDetailMapper undrugDetailMapper,
                    ClinicInvoiceInfoMapper clinicInvoiceInfoMapper, IiBeduserecordMapper iiBeduserecordMapper,
                    IiShiftdataMapper iiShiftdataMapper, IncomeExpensesMapper incomeExpensesMapper,
                    LlOneBackExpensesMapper llOneBackExpensesMapper, MrmsbaseMapper mrmsbaseMapper, MrmsdymrMapper mrmsdymrMapper,
                    OpApplyMapper opApplyMapper, OrdSeedoctrecordMapper ordSeedoctrecordMapper, RMzClassCollectYardMapper rMzClassCollectYardMapper,
                    RStatMzClassCollectMapper rStatMzClassCollectMapper, RStatMzCollectMapper rStatMzCollectMapper, VmHdTreatMapper vmHdTreatMapper, VmHdTreatXgMapper vmHdTreatXGMapper, MedOperationMasterMapper medOperationMasterMapper, CpoeProjectDischargeExeMapper cpoeProjectDischargeExeMapper, OpRecordMapper opRecordMapper, RRegisterMapper rRegisterMapper, VHljxFeedetailMapper vHljxFeedetailMapper, VCssdWorkloadMapper vCssdWorkloadMapper) {
        this.jobRepository = jobRepository;
        this.transactionManager = transactionManager;
        this.ifFeeItemMapper = ifFeeItemMapper;
        this.ipmomdodMapper = ipmomdodMapper;
        this.ipmonmexMapper = ipmonmexMapper;
        this.undrugDetailMapper = undrugDetailMapper;
        this.clinicInvoiceInfoMapper = clinicInvoiceInfoMapper;
        this.iiBeduserecordMapper = iiBeduserecordMapper;
        this.iiShiftdataMapper = iiShiftdataMapper;
        this.incomeExpensesMapper = incomeExpensesMapper;
        this.llOneBackExpensesMapper = llOneBackExpensesMapper;
        this.mrmsbaseMapper = mrmsbaseMapper;
        this.mrmsdymrMapper = mrmsdymrMapper;
        this.opApplyMapper = opApplyMapper;
        this.ordSeedoctrecordMapper = ordSeedoctrecordMapper;
        this.rMzClassCollectYardMapper = rMzClassCollectYardMapper;
        this.rStatMzClassCollectMapper = rStatMzClassCollectMapper;
        this.rStatMzCollectMapper = rStatMzCollectMapper;
        this.vmHdTreatMapper = vmHdTreatMapper;
        this.vmHdTreatXGMapper = vmHdTreatXGMapper;
        this.medOperationMasterMapper = medOperationMasterMapper;
        this.cpoeProjectDischargeExeMapper = cpoeProjectDischargeExeMapper;
        this.opRecordMapper = opRecordMapper;
        this.rRegisterMapper = rRegisterMapper;
        this.vHljxFeedetailMapper = vHljxFeedetailMapper;
        this.vCssdWorkloadMapper = vCssdWorkloadMapper;
    }

    @Bean("adviceItemStep")
    public org.springframework.batch.core.Step step(@Qualifier("adviceItemReader") MyBatisCursorItemReader<AdviceItem> itemReaderMybatis,
                                                    @Qualifier("adviceItemWriter") MyBatisBatchItemWriter<AdviceItem> itemWriterMybatis) {
        return new StepBuilder("adviceItemStep", jobRepository)
                .<AdviceItem, AdviceItem>chunk(1000, transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteAdviceItemStep")
    @Transactional
    public Step deleteStep(@Qualifier("dataSource") DataSource dataSource) {
        return new StepBuilder("deleteStep", jobRepository)

                .tasklet((contribution, chunkContext) -> {
                    // 执行删除操作
                    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
                    jdbcTemplate.update("TRUNCATE table advice_item");
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .build();
    }

    @Bean("deleteIfFeeItem")
    @Transactional
    public Step deleteIfFeeItem(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIfFeeItem", "IF_FEEITEMLIST");
    }

    @Bean("ifFeeitemListStep")
    @Transactional
    public Step ifFeeitemListStep(@Qualifier("ifFreeItemItemReader") MyBatisCursorItemReader<IfFeeitemlist> itemReaderMybatis,
                                  @Qualifier("ifFreeItemItemWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "ifFeeitemListStep", cachedItems.get(0));
    }

    @Bean("ifFeeitemListStep2")
    @Transactional
    public Step ifFeeitemListStep2() {
        return new StepBuilder("ifFeeitemListStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(0)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        ifFeeItemMapper.insertAll(cachedItems.get(0));
                    }
                    cachedItems.get(0)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

//    @Bean("deleteAdviceItemStep")
//    @Transactional
//    public Step test(@Qualifier("dataSource") DataSource dataSource) {
//        return new StepBuilder("deleteStep", jobRepository)
//
//                .tasklet((contribution, chunkContext) -> {
//                    // 获取作业参数
//                    JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
//                    String someValue = jobParameters.getString("someValue");
//
//                    // 构建带有 WHERE 条件的 SQL 语句
//                    String sql = "DELETE FROM advice_item WHERE some_column = :someValue";
//
//                    // 设置SQL参数
//                    MapSqlParameterSource parameters = new MapSqlParameterSource();
//                    parameters.addValue("someValue", someValue);
//
//                    // 执行删除操作
//                    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
//                    jdbcTemplate.update(sql, parameters);
//
//                    return RepeatStatus.FINISHED;
//                },transactionManager)
//                .build();
//    }


    @Bean("deleteIpMomDodStep")
    @Transactional
    public Step deleteIpMomDod(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIpMomDodStep", "IPMOMDOD");
    }

    @Bean("ipMomDodStep1")
    @Transactional
    public Step ipMomDodStep1(@Qualifier("ipMomDodReader") MyBatisCursorItemReader<Ipmomdod> itemReaderMybatis,
                              @Qualifier("ipMomDodWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {

        return getStep1(itemReaderMybatis, itemWriterMybatis, "ipMomDodStep1", cachedItems.get(1));
    }

    @Bean("ipMomDodStep2")
    @Transactional
    public Step ipMomDodStep2() {
        return new StepBuilder("ipMomDodStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(1)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        ipmomdodMapper.insertAll(cachedItems.get(1));
                    }
                    cachedItems.get(1)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteIpMonMexStep")
    @Transactional
    public Step deleteIpMonMex(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIpMomDodStep", "IPMONMEX");
    }

    @Bean("ipMonMexStep1")
    @Transactional
    public Step ipMonMexStep1(@Qualifier("ipMonMexReader") MyBatisCursorItemReader<Ipmonmex> itemReaderMybatis,
                              @Qualifier("ipMonMexWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "ipMonMexStep1", cachedItems.get(2));
    }

    @Bean("ipMonMexStep2")
    @Transactional
    public Step ipMonMexStep2() {
        return new StepBuilder("ipMonMexStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(2)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        ipmonmexMapper.insertAll(cachedItems.get(2));
                    }
                    cachedItems.get(2)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteUndrugDetailStep")
    @Transactional
    public Step deleteUndrugDetail(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteUndrugDetailStep", "UNDRUG_DETAIL");
    }

    @Bean("undrugDetailStep1")
    @Transactional
    public Step undrugDetailStep1(@Qualifier("unDrugDetailReader") MyBatisCursorItemReader<UndrugDetail> itemReaderMybatis,
                                  @Qualifier("unDrugDetailWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "undrugDetailStep1", cachedItems.get(3));
    }

    @Bean("undrugDetailStep2")
    @Transactional
    public Step undrugDetailStep2() {
        return new StepBuilder("undrugDetailStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(3)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        undrugDetailMapper.insertAll(cachedItems.get(3));
                    }
                    cachedItems.get(3)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }


    @Bean("deleteClinicInvoiceStep")
    @Transactional
    public Step deleteClinicInvoice(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteClinicInvoiceStep", "clinic_invoice_info");
    }


    @Bean("clinicInvoiceStep1")
    @Transactional
    public Step clinicInvoiceStep1(@Qualifier("clinicInvoiceReader") MyBatisCursorItemReader<ClinicInvoiceInfo> itemReaderMybatis,
                                   @Qualifier("clinicInvoiceWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "clinicInvoiceStep1", cachedItems.get(4));
    }

    @Bean("clinicInvoiceStep2")
    @Transactional
    public Step clinicInvoiceStep2() {
        return new StepBuilder("clinicInvoiceStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(4)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        clinicInvoiceInfoMapper.insertAll(cachedItems.get(4));
                    }
                    cachedItems.get(4)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteIiBedUseRecordStep")
    @Transactional
    public Step deleteIiBedUseRecord(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIiBedUseRecordStep", "ii_beduserecord");
    }


    @Bean("iiBedUseRecordStep1")
    @Transactional
    public Step iiBedUseRecordStep1(@Qualifier("iiBedUseRecordReader") MyBatisCursorItemReader<IiBeduserecord> itemReaderMybatis,
                                    @Qualifier("iiBedUseRecordWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "iiBedUseRecordStep1", cachedItems.get(5));
    }

    @Bean("iiBedUseRecordStep2")
    @Transactional
    public Step iiBedUseRecordStep2() {
        return new StepBuilder("iiBedUseRecordStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(5)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        iiBeduserecordMapper.insertAll(cachedItems.get(5));
                    }
                    cachedItems.get(5)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteIiShiftDataStep")
    @Transactional
    public Step deleteIiShiftDataStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIiShiftDataStep", "ii_shiftdata");
    }


    @Bean("iiShiftDataStep1")
    @Transactional
    public Step iiShiftDataStep1(@Qualifier("iiShiftDataReader") MyBatisCursorItemReader<IiShiftdata> itemReaderMybatis,
                                 @Qualifier("iiShiftDataWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "iiShiftDataStep1", cachedItems.get(6));
    }

    @Bean("iiShiftDataStep2")
    @Transactional
    public Step iiShiftDataStep2() {
        return new StepBuilder("iiShiftDataStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(6)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        iiShiftdataMapper.insertAll(cachedItems.get(6));
                    }
                    cachedItems.get(6)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteIncomeExpensesStep")
    @Transactional
    public Step deleteIncomeExpensesStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteIncomeExpensesStep", "income_expenses");
    }


    @Bean("incomeExpensesStep1")
    @Transactional
    public Step incomeExpensesStep1(@Qualifier("incomeExpensesReader") MyBatisCursorItemReader<IncomeExpenses> itemReaderMybatis,
                                    @Qualifier("incomeExpensesWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "incomeExpensesStep1", cachedItems.get(7));
    }

    @Bean("incomeExpensesStep2")
    @Transactional
    public Step incomeExpensesStep2() {
        return new StepBuilder("incomeExpensesStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(7)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        incomeExpensesMapper.insertAll(cachedItems.get(7));
                    }
                    cachedItems.get(7)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteLlOneBackExpensesStep")
    @Transactional
    public Step deleteLlOneBackExpensesStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteLlOneBackExpensesStep", "ll_one_back_expenses");
    }


    @Bean("llOneBackExpensesStep1")
    @Transactional
    public Step llOneBackExpensesStep1(@Qualifier("llOneBackExpensesReader") MyBatisCursorItemReader<LlOneBackExpenses> itemReaderMybatis,
                                       @Qualifier("llOneBackExpensesWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "llOneBackExpensesStep1", cachedItems.get(8));
    }

    @Bean("llOneBackExpensesStep2")
    @Transactional
    public Step llOneBackExpensesStep2() {
        return new StepBuilder("llOneBackExpensesStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(8)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        llOneBackExpensesMapper.insertAll(cachedItems.get(8));
                    }
                    cachedItems.get(8)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteMrmsbaseStep")
    @Transactional
    public Step deleteMrmsbaseStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteMrmsbaseStep", "mrmsbase");
    }


    @Bean("mrmsbaseStep1")
    @Transactional
    public Step mrmsbaseStep1(@Qualifier("mrmsbaseReader") MyBatisCursorItemReader<Mrmsbase> itemReaderMybatis,
                              @Qualifier("mrmsbaseWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "mrmsbaseStep1", cachedItems.get(9));
    }

    @Bean("mrmsbaseStep2")
    @Transactional
    public Step mrmsbaseStep2() {
        return new StepBuilder("mrmsbaseStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(9)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        mrmsbaseMapper.insertAll(cachedItems.get(9));
                    }
                    cachedItems.get(9)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteMrmsdymrStep")
    @Transactional
    public Step deleteMrmsdymrStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteMrmsdymrStep", "mrmsdymr");
    }


    @Bean("mrmsdymrStep1")
    @Transactional
    public Step mrmsdymrStep1(@Qualifier("mrmsdymrReader") MyBatisCursorItemReader<Mrmsdymr> itemReaderMybatis,
                              @Qualifier("mrmsdymrWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "mrmsdymrStep1", cachedItems.get(10));
    }

    @Bean("mrmsdymrStep2")
    @Transactional
    public Step mrmsdymrStep2() {
        return new StepBuilder("mrmsdymrStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(10)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        mrmsdymrMapper.insertAll(cachedItems.get(10));
                    }
                    cachedItems.get(10)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }
    @Bean("deleteOpRecordStep")
    @Transactional
    public Step deleteOpRecordStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteOpRecordStep", "op_record");
    }
    @Bean("opRecordStep1")
    @Transactional
    public Step opRecordStep1(@Qualifier("opRecordReader") MyBatisCursorItemReader<OpRecord> itemReaderMybatis,
                              @Qualifier("opRecordWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "opRecordStep1", cachedItems.get(19));
    }
    @Bean("opRecordStep2")
    @Transactional
    public Step opRecordStep2() {
        return new StepBuilder("opRecordStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(19)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        opRecordMapper.insertOpRecord(cachedItems.get(19));
                    }
                    cachedItems.get(19)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }
    @Bean("deleteCpoeProjectDischargeExeStep")
    @Transactional
    public Step deleteCpoeProjectDischargeExeStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteCpoeProjectDischargeExeStep", "cpoe_project_discharge_exe");
    }
    @Bean("cpoeProjectDischargeExeStep1")
    @Transactional
    public Step cpoeProjectDischargeExeStep1(@Qualifier("CpoeProjectDischargeExeReader") MyBatisCursorItemReader<CpoeProjectDischargeExe> itemReaderMybatis,
                             @Qualifier("CpoeProjectDischargeExeWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "cpoeProjectDischargeExeStep1", cachedItems.get(18));
    }

    @Bean("cpoeProjectDischargeExeStep2")
    @Transactional
    public Step cpoeProjectDischargeExeStep2() {
        return new StepBuilder("cpoeProjectDischargeExeStep1", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(18)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        cpoeProjectDischargeExeMapper.insertAll(cachedItems.get(18));
                    }
                    cachedItems.get(18)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteRRegisterStep")
    @Transactional
    public Step deleteRRegisterStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteRRegisterStep", "r_register");
    }
    @Bean("rRegisterStep1")
    @Transactional
    public Step rRegisterStep1(@Qualifier("rRegisterReader") MyBatisCursorItemReader<RRegister> itemReaderMybatis,
                               @Qualifier("rRegisterWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "rRegisterStep1", cachedItems.get(20));

    }

    @Bean("rRegisterStep2")
    @Transactional
    public Step rRegisterStep2() {
        return new StepBuilder("rRegisterStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(20)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        rRegisterMapper.insertAll(cachedItems.get(20));
                    }
                    cachedItems.get(20)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
        }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }
    @Bean("deleteVHljxFeedetailStep")
    @Transactional
    public Step deleteVHljxFeedetailStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteVHljxFeedetailStep", "v_hljx_feedetail");
    }
    @Bean("vHljxFeedetailStep1")
    @Transactional
    public Step vHljxFeedetailStep1(@Qualifier("vHljxFeedetailReader") MyBatisCursorItemReader<VHljxFeedetail> itemReaderMybatis,
                                    @Qualifier("vHljxFeedetailWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "vHljxFeedetailStep1", cachedItems.get(21));
                                    }
    @Bean("vHljxFeedetailStep2")
    @Transactional
    public Step vHljxFeedetailStep2() {
        return new StepBuilder("vHljxFeedetailStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(21)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        vHljxFeedetailMapper.insertAll(cachedItems.get(21));
                    }
                    cachedItems.get(21).getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
        }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }
    @Bean("deleteOpApplyStep")
    @Transactional
    public Step deleteOpApplyStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteOpApplyStep", "op_apply");
    }




    @Bean("opApplyStep1")
    @Transactional
    public Step opApplyStep1(@Qualifier("opApplyReader") MyBatisCursorItemReader<OpApply> itemReaderMybatis,
                             @Qualifier("opApplyWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "opApplyStep1", cachedItems.get(11));
    }

    @Bean("opApplyStep2")
    @Transactional
    public Step opApplyStep2() {
        return new StepBuilder("opApplyStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(11)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        opApplyMapper.insertAll(cachedItems.get(11));
                    }
                    cachedItems.get(11)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteOrdSeedoctrecordStep")
    @Transactional
    public Step deleteOrdSeedoctrecordStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteOrdSeedoctrecordStep", "ord_seedoctrecord");
    }


    @Bean("ordSeedoctrecordStep1")
    @Transactional
    public Step ordSeedoctrecordStep1(@Qualifier("ordSeedoctrecordReader") MyBatisCursorItemReader<OrdSeedoctrecord> itemReaderMybatis,
                                      @Qualifier("ordSeedoctrecordWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "ordSeedoctrecordStep1", cachedItems.get(12));
    }

    @Bean("ordSeedoctrecordStep2")
    @Transactional
    public Step ordSeedoctrecordStep2() {
        return new StepBuilder("ordSeedoctrecordStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(12)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        ordSeedoctrecordMapper.insertAll(cachedItems.get(12));
                    }
                    cachedItems.get(12)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteRMzClassCollectYardStep")
    @Transactional
    public Step deleteRMzClassCollectYardStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteRMzClassCollectYardStep", "r_mz_class_collect_yard");
    }


    @Bean("rMzClassCollectYardStep1")
    @Transactional
    public Step rMzClassCollectYardStep1(@Qualifier("rMzClassCollectYardReader") MyBatisCursorItemReader<RMzClassCollectYard> itemReaderMybatis,
                                         @Qualifier("rMzClassCollectYardWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "rMzClassCollectYardStep1", cachedItems.get(13));
    }

    @Bean("rMzClassCollectYardStep2")
    @Transactional
    public Step rMzClassCollectYardStep2() {
        return new StepBuilder("rMzClassCollectYardStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(13)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        rMzClassCollectYardMapper.insertAll(cachedItems.get(13));
                    }
                    cachedItems.get(13)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteRStatMzClassCollectStep")
    @Transactional
    public Step deleteRStatMzClassCollectStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteRStatMzClassCollectStep", "r_stat_mz_class_collect");
    }


    @Bean("rStatMzClassCollectStep1")
    @Transactional
    public Step rStatMzClassCollectStep1(@Qualifier("rStatMzClassCollectReader") MyBatisCursorItemReader<RStatMzClassCollect> itemReaderMybatis,
                                         @Qualifier("rStatMzClassCollectWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "rStatMzClassCollectStep1", cachedItems.get(14));
    }

    @Bean("rStatMzClassCollectStep2")
    @Transactional
    public Step rStatMzClassCollectStep2() {
        return new StepBuilder("rStatMzClassCollectStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(14)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        rStatMzClassCollectMapper.insertAll(cachedItems.get(14));
                    }
                    cachedItems.get(14)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteRStatMzCollectStep")
    @Transactional
    public Step deleteRStatMzCollectStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteRStatMzCollectStep", "r_stat_mz_collect");
    }


    @Bean("rStatMzCollectStep1")
    @Transactional
    public Step rStatMzCollectStep1(@Qualifier("rStatMzCollectReader") MyBatisCursorItemReader<RStatMzCollect> itemReaderMybatis,
                                    @Qualifier("rStatMzCollectWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "rStatMzCollectStep1", cachedItems.get(15));
    }

    @Bean("rStatMzCollectStep2")
    @Transactional
    public Step rStatMzCollectStep2() {
        return new StepBuilder("rStatMzCollectStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(15)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        rStatMzCollectMapper.insertAll(cachedItems.get(15));
                    }
                    cachedItems.get(15)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }






    @Bean("deleteVmHdTreatStep")
    @Transactional
    public Step deleteVmHdTreatStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteVmHdTreatStep", "vm_hd_treat");
    }


    @Bean("VmHdTreatStep1")
    @Transactional
    public Step VmHdTreatStep1(@Qualifier("VmHdTreatRead") MyBatisCursorItemReader<VmHdTreat> itemReaderMybatis,
                                    @Qualifier("VmHdTreatWriterMybatis") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "VmHdTreatStep1", cachedItems.get(16));
    }

    @Bean("VmHdTreatStep2")
    @Transactional
    public Step VmHdTreatStep2() {
        return new StepBuilder("VmHdTreatStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(16)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        vmHdTreatMapper.insertAll(cachedItems.get(16));
                    }
                    cachedItems.get(16)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteVmHdTreatXGStep")
    @Transactional
    public Step deleteVmHdTreatXGStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteVmHdTreatXGStep", "vm_hd_treat_xg");
    }


    @Bean("VmHdTreatXGStep1")
    @Transactional
    public Step VmHdTreatXGStep1(@Qualifier("VmHdTreatXgRead") MyBatisCursorItemReader<VmHdTreatXg> itemReaderMybatis,
                               @Qualifier("VmHdTreatXgWriterMybatis") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "VmHdTreatXGStep1", cachedItems.get(22));
    }

    @Bean("VmHdTreatXGStep2")
    @Transactional
    public Step VmHdTreatXGStep2() {
        return new StepBuilder("VmHdTreatXGStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(22)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        vmHdTreatXGMapper.insertAll(cachedItems.get(22));
                    }
                    cachedItems.get(22)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }







    @Bean("deleteMedOperationMasterStep")
    @Transactional
    public Step deleteMedOperationMasterStep(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteMedOperationMasterStep", "med_operation_master");
    }


    @Bean("MedOperationMasterStep1")
    @Transactional
    public Step MedOperationMasterStep1(@Qualifier("MedOperationMasterRead") MyBatisCursorItemReader<MedOperationMaster> itemReaderMybatis,
                               @Qualifier("MedOperationMasterWriterMybatis") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "MedOperationMasterStep1", cachedItems.get(17));
    }

    @Bean("MedOperationMasterStep2")
    @Transactional
    public Step MedOperationMasterStep2() {
        return new StepBuilder("MedOperationMasterStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(17)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        medOperationMasterMapper.insertAll(cachedItems.get(17));
                    }
                    cachedItems.get(17)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }











    @Bean("initIaFeelist")
    @Transactional
    public Step initIaFeelist(@Qualifier("dataSource") DataSource dataSource) {
        String stepName = "initIaFeelistStep", tableName = "ia_feelist";
        return getTaskletStep(dataSource, stepName, tableName);
    }

    @Bean("initMrmsbasetime")
    @Transactional
    public Step initMrmsbasetimeStep(@Qualifier("dataSource") DataSource dataSource) {
        String stepName = "initMrmsbasetimeStep", tableName = "mrmsbasetime";
        return getTaskletStep(dataSource, stepName, tableName);
    }

    @Bean("initPrivDept")
    @Transactional
    public Step initPrivDeptStep(@Qualifier("dataSource") DataSource dataSource) {
        String stepName = "initPrivDeptStep", tableName = "priv_dept";
        return getTaskletStep(dataSource, stepName, tableName);
    }

    @Bean("initRDepartment")
    @Transactional
    public Step initRDepartmentStep(@Qualifier("dataSource") DataSource dataSource) {
        String stepName = "initRDepartmentStep", tableName = "r_department";
        return getTaskletStep(dataSource, stepName, tableName);
    }

    @Bean("initREmployee")
    @Transactional
    public Step initREmployee(@Qualifier("dataSource") DataSource dataSource) {
        String stepName = "initREmployeeStep", tableName = "r_employee";
        return getTaskletStep(dataSource, stepName, tableName);
    }

    @Bean("saveIaFeelist")
    public Step saveIaFeelist(@Qualifier("iaFeelistRead") MyBatisCursorItemReader<IaFeelist> itemReaderMybatis,
                              @Qualifier("iaFeelistWriterMybatis") MyBatisBatchItemWriter<IaFeelist> itemWriterMybatis) {
        return new StepBuilder("saveIaFeelistStep", jobRepository).<IaFeelist, IaFeelist>chunk(1000, transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("saveMrmsbasetime")
    public Step saveMrmsbasetime(@Qualifier("mrmsbasetimeRead") MyBatisCursorItemReader<Mrmsbasetime> itemReaderMybatis,
                              @Qualifier("mrmsbasetimeWriterMybatis") MyBatisBatchItemWriter<Mrmsbasetime> itemWriterMybatis) {
        return new StepBuilder("saveMrmsbasetimeStep", jobRepository).<Mrmsbasetime, Mrmsbasetime>chunk(1000,
                        transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("savePrivDept")
    public Step savePrivDept(@Qualifier("privDeptRead") MyBatisCursorItemReader<PrivDept> itemReaderMybatis,
                              @Qualifier("privDeptWriterMybatis") MyBatisBatchItemWriter<PrivDept> itemWriterMybatis) {
        return new StepBuilder("savePrivDeptStep", jobRepository).<PrivDept, PrivDept>chunk(1000, transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("saveRDepartment")
    public Step saveRDepartment(@Qualifier("rDepartmentRead") MyBatisCursorItemReader<RDepartment> itemReaderMybatis,
                              @Qualifier("rDepartmentWriterMybatis") MyBatisBatchItemWriter<RDepartment> itemWriterMybatis) {
        return new StepBuilder("saveRDepartmentStep", jobRepository).<RDepartment, RDepartment>chunk(1000,
                        transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("saveREmployee")
    public Step saveREmployee(@Qualifier("rEmployeeRead") MyBatisCursorItemReader<REmployee> itemReaderMybatis,
                              @Qualifier("rEmployeeWriterMybatis") MyBatisBatchItemWriter<REmployee> itemWriterMybatis) {
        return new StepBuilder("saveREmployeeStep", jobRepository).<REmployee, REmployee>chunk(1000, transactionManager)
                .reader(itemReaderMybatis)
                .writer(itemWriterMybatis)
                .allowStartIfComplete(true)
                .build();
    }

    @Bean("deleteVCssdWorkload")
    @Transactional
    public Step deleteVCssdWorkload(@Qualifier("dataSource") DataSource dataSource) {
        return getDelTep(dataSource, "deleteVCssdWorkload", "v_cssd_workload");
    }

    @Bean("vCssdWorkloadStep1")
    @Transactional
    public Step vCssdWorkloadStep1(@Qualifier("vCssdWorkloadReader") MyBatisCursorItemReader<vCssdWorkload> itemReaderMybatis,
                                  @Qualifier("vCssdWorkloadWriter") MyBatisBatchItemWriter<ListItem> itemWriterMybatis) {
        return getStep1(itemReaderMybatis, itemWriterMybatis, "vCssdWorkloadStep1", cachedItems.get(23));
    }

    @Bean("vCssdWorkloadStep2")
    @Transactional
    public Step vCssdWorkloadStep2() {
        return new StepBuilder("vCssdWorkloadStep2", jobRepository).tasklet((contribution, chunkContext) -> {
                    if (!cachedItems.get(23)
                            .getItems()
                            .isEmpty()) {
                        // 不为空的时候再次插入
                        vCssdWorkloadMapper.insertAll(cachedItems.get(23));
                    }
                    cachedItems.get(23)
                            .getItems()
                            .clear();
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .allowStartIfComplete(true)
                .build();
    }


    private TaskletStep getTaskletStep(DataSource dataSource, String stepName, String tableName) {
        return new StepBuilder(stepName, jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    // 执行删除操作
                    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
                    jdbcTemplate.update("TRUNCATE table " + tableName);
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .build();
    }

    private TaskletStep getDelTep(DataSource dataSource, String stepName, String tableName) {
        return new StepBuilder(stepName, jobRepository).
                tasklet(((contribution, chunkContext) -> {
                    JobParameters jobParameters = chunkContext.getStepContext()
                            .getStepExecution()
                            .getJobParameters();
                    String splitTableName = jobParameters.getString("splitTableName");

                    String sql = "TRUNCATE table " + tableName + "_" + splitTableName;

                    // 设置SQL参数
                    MapSqlParameterSource parameters = new MapSqlParameterSource();

                    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
                    jdbcTemplate.update(sql, parameters);
                    return RepeatStatus.FINISHED;
                }), transactionManager)
                .build();
    }

    @Synchronized
    private <T extends SplitTableName> TaskletStep getStep1(MyBatisCursorItemReader<T> itemReaderMybatis, MyBatisBatchItemWriter<ListItem> itemWriterMybatis,
                                                            String beanName, ListItem cachedList) {
        return new StepBuilder(beanName, jobRepository)
                .<T, ListItem>chunk(1000, transactionManager)
                .reader(itemReaderMybatis)
                .processor(item -> {
                    if (cachedList.getItems()
                            .size() >= 1000) {
                        cachedList.getItems()
                                .clear();
                    }
                    cachedList.getItems()
                            .add(item);
                    cachedList.setSplitTableName(item.getSplitTableName());
                    if (cachedList.getItems()
                            .size() >= 1000) {
                        return cachedList;
                    }
                    return null;
                })
                .writer(itemWriterMybatis)
//                 .taskExecutor(new SimpleAsyncTaskExecutor())
                .allowStartIfComplete(true)
                .build();
    }


}
